import CryptoJS from 'crypto-js'

export const encrypt = (plaintText: string) => {
  const CRYPTOJSKEY = 'cmbCampus0722000'
  const options = { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }
  const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY)
  const encryptedData = CryptoJS.AES.encrypt(plaintText, key, options)
  const encryptedBase64Str = encryptedData.toString()
  return encryptedBase64Str
}
export const MySign = {
  //加密/解密次数
  num: 0,
  //加密
  Encrypt: function (Text: any) {
    this.num = this.num + 1
    let output = new String()
    const alterText = []
    const varCost = []
    const TextSize = Text.length
    for (let i = 0; i < TextSize; i++) {
      const idea = Math.round(Math.random() * 111) + 77
      alterText[i] = Text.charCodeAt(i) + idea
      varCost[i] = idea
    }
    for (let i = 0; i < TextSize; i++) {
      output += String.fromCharCode(alterText[i], varCost[i])
    }
    //text1.value = output;
    return output
  },

  //解密
  UnEncrypt: function (Text: any) {
    if (this.num > 0) {
      this.num = this.num - 1
      let output = new String()
      const alterText = []
      const varCost = []
      const TextSize = Text.length
      for (let i = 0; i < TextSize; i++) {
        alterText[i] = Text.charCodeAt(i)
        varCost[i] = Text.charCodeAt(i + 1)
      }
      for (let i = 0; i < TextSize; i = i + 2) {
        output += String.fromCharCode(alterText[i] - varCost[i])
      }
      //text1.value = output;
      return output
    }
  },
}
